<?php
namespace App\Model;


use App\Utility\SysConst;
use Core\AbstractInterface\AbstractAsyncTask;
use Core\Component\Logger;
use Core\Component\ShareMemory;
use Core\Utility\Curl\Request;

class Runner extends AbstractAsyncTask
{

    function handler(\swoole_server $server, $taskId, $fromId)
    {
        // TODO: Implement handler() method.
        //记录处于运行状态的task数量
        $share = ShareMemory::getInstance();
        $share->startTransaction();
        $share->set(SysConst::TASK_RUNNING_NUM,$share->get(SysConst::TASK_RUNNING_NUM)+1);
        $share->commit();
        //while其实为危险操作，while会剥夺进程控制权
        $i=0;
        $phpscript = dirname(ROOT).'/quickTaskQueue.php';
        while ($i<100){
            $i++;
            $tid = Queue::pop();
            if (!empty($tid)) {
                $env = PLATFORM.'-'.ENV;
                $table = TABLE_NAME;
                exec("timeout -s 15 php $phpscript {$tid} {$env} {$table}", $output, $return);
                //Logger::getInstance("task")->console("finish tid:".$tid);
            } else{
                echo 'break'."\n";
                break;
            }
        }

        $share->startTransaction();
        $share->set(SysConst::TASK_RUNNING_NUM,$share->get(SysConst::TASK_RUNNING_NUM)-1);
        $share->commit();
    }

    function finishCallBack(\swoole_server $server, $task_id, $resultData)
    {
        // TODO: Implement finishCallBack() method.
    }
}
